package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.constants.CommonConstant;
import com.dmcb.common.business.utils.DateUtil;
import com.dmcb.common.business.utils.JsonUtil;
import com.dmcb.common.business.utils.ListUtil;
import com.dmcb.common.business.utils.WebUtil;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.crawlers.article.ZheJiang24HourParser;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.entities.Author;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 浙江24小时文章全部导入
 * Created by 黑米 on 2017/3/16.
 */
@Service
public class ZheJiang24HourCrawler extends BaseCrawler {
    Logger logger = LoggerFactory.getLogger(ZheJiang24HourCrawler.class);

    @Autowired
    private ZheJiang24HourParser zheJiang24HourParser;
    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;

    @Override
    protected String url() {
        return CrawlConstant.ZHEJIANG24HOUR_URL;
    }

    @Override
    protected int tag() {
        return CrawlConstant.ZHEJIANG24HOUR_TAG;
    }

    @Override
    protected String avatarSelect() {
        return "a.avatar>img";
    }

    @Override
    protected String channelNameSelect() {
        return "div.title>a";
    }

    /**
     * 频道名称是否同时是作者名称
     *
     * @return 是、否
     */
    @Override
    protected boolean isAuthorName() {
        return true;
    }

    @Override
    protected String authorLink() {
        return null;
    }

    @Override
    protected String listSelect() {
        return "ul.note-list>li";
    }

    @Override
    protected String titleSelect() {
        return "div.content>a";
    }

    @Override
    protected String authorSelect() {
        return "div.title>a";
    }
    // Cache-Control: max-stale=2147483647, only-if-cached
    //{"lastMinPublishTime":1489562063061}
    //http://app.thehour.cn/h24/app/v0/homepage/day/loadmore  日子
    //http://app.thehour.cn/h24/app/v0/homepage/news/loadmore  新闻

    //刷新
    //                     /h24/app/v0/homepage/news/refresh


    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> getList(String json) {
        if (StringUtils.isBlank(json)) {
            return null;
        }
        Map<String, Object> map = JsonUtil.getResult(json);
        if (map.isEmpty()) {
            return null;
        }
        map = (Map) map.get("data");
        if (map.isEmpty()) {
            return null;
        }
        return (List) map.get("articleList");
    }

    @Override
    @SuppressWarnings("unchecked")
    protected JsonResult crawl(int authorId, String link, Author author, Document doc, boolean all) {
        // 爬取文章
        int totalAll = 0;
        Long unixTime = DateUtil.getUnixTime();
        Map<String, String> map = new HashMap<String, String>();
        map.put("lastRefreshTime", unixTime + "");
        String[] types = new String[]{"day", "news"};
        try {
            for (String type : types) {
                String json = WebUtil.sendJsonPost("http://app.thehour.cn/h24/app/v0/homepage/" + type + "/refresh", map, "application/json; charset=utf-8");
                List<Map<String, Object>> mapList = getList(json);
                int ero = 0;
                while (true) {
                    if (ListUtil.isEmpty(mapList)) {
                        ero++;
                        Thread.sleep(60000);
                        if (ero > 5) {
                            break;
                        }
                    }
                    ero = 0;
                    Map<String, Object> lastMap = (Map) mapList.get(mapList.size() - 1);
                    Long lastTime = (Long) lastMap.get("sortNum");
                    if(lastTime == null){
                        break;
                    }
                    JsonResult result = iterate(authorId, mapList);
                    if (!result.isSuccess()) {
                        return result;
                    }
                    totalAll += result.getInt();
                    if (!all && result.getInt() < 1) {
                        break;
                    }
                    map.clear();
                    map.put("lastMinPublishTime", lastTime + "");
                    json = WebUtil.sendJsonPost("http://app.thehour.cn/h24/app/v0/homepage/" + type + "/loadmore", map, "application/json; charset=utf-8");
                    mapList = getList(json);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return totalAll == 0 ? error(CrawlConstant.MSG_WARN_NO_UPDATE) : success(totalAll);
    }

    /**
     * 迭代解析、保存文章
     *
     * @return 成功保存数量
     */
    protected JsonResult iterate(int authorId, List<Map<String, Object>> maps) {
        int succ = 0;
        int exist = 0;
        for (Map<String, Object> map : maps) {
            // 获取标题、链接元素
            String title = (String) map.get("listTitle");
            if (StringUtils.isNotBlank(title)) {
                try {
                    // 校验作者、标题是否已存在,
                    //浙江24小时APP的文章属浙江日报旗下，与其微信运营公众号很多文章可能重复，
                    //在此先监测与钱江晚报账号旗下运营的微信公众号里面的所有文章，对比，作者id：2268
                    Article article = articleCrawlMapper.queryByAuthorTitle(2268, title.trim());
                    if (article != null) {
                        exist++;
                        if (exist >= 6 && succ == 0) {
                            return success(0);
                        }
                        continue;
                    }
                    int id = (Integer) map.get("id");
                    String href = "http://app.thehour.cn/wap/share.html?id=" + id;
                    JsonResult result = parse(authorId, href);
                    succ += result.isSuccess() ? 1 : 0;
                } catch (Exception e) {
                    logger.error("Parser.parse error,{}", map.toString(), e);
                    return error(CommonConstant.ABNORMAL_MSG);
                }
            }
        }
        return success(succ);
    }


    @Override
    protected JsonResult parse(int authorId, String link) throws Exception {
        return zheJiang24HourParser.parse(authorId, link);
    }

    @Override
    protected String getBody(Document doc) {
        return zheJiang24HourParser.getBody(doc);
    }
}
